package com.binarycube.webserver.config;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.binarycube.webserver.ClazzLoader;
import com.binarycube.webserver.authenticator.IAuthenticator;
import com.binarycube.webserver.session.ISessionManager;

public class ServerConfig {
	private  int port;
	private String name;
	private String serverRoot;
	private String pageBase;
	private String scriptBase;
	private String welcome;
	private String loginURL;
	
	private Date loadedTS;
	
	

	private SessionManagerConfig sessionManager;
	private AuthenticatorConfig authenticator;
	private ISessionManager sessionManagerInstance = null;
	private IAuthenticator authenticatorInstance = null;
	
	private Map<String, ScriptConfig> scripts;
	private Map<String, PageConfig> pages;
	
	
	public ServerConfig(){
		pages = new HashMap<String, PageConfig>();
		scripts = new HashMap<String, ScriptConfig>();
		
	}
	
	
	public int getPort() {
		return port;
	}
	public void setPort(int serverPort) {
		this.port = serverPort;
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getServerRoot() {
		return serverRoot;
	}
	public void setServerRoot(String root) {
		this.serverRoot = root;
	}
	public String getPageBase() {
		return pageBase;
	}
	public void setPageBase(String pageBase) {
		this.pageBase = pageBase;
	}
	
	
	
	public String getWelcome() {
		return welcome;
	}


	public void setWelcome(String welcome) {
		this.welcome = welcome;
	}

	
	

	public String getLoginURL() {
		return loginURL;
	}


	public void setLoginURL(String loginURL) {
		this.loginURL = loginURL;
	}


	public String getScriptBase() {
		return scriptBase;
	}


	public void setScriptBase(String scriptBase) {
		this.scriptBase = scriptBase;
	}


	
	
	public void addPage(PageConfig pc) {
		pages.put(pc.getName(), pc);
	}

	public void addScript(ScriptConfig sc) {
		scripts.put(sc.getName(), sc);
	}
	

	public PageConfig getPage(String name) {
		return pages.get(name);
	}
	


	public ScriptConfig getScript(String name) {
		return scripts.get(name);
	}
	
	public void setSessionManager(SessionManagerConfig smc){
		sessionManager = smc;
	}
	
	public void setAuthenticator(AuthenticatorConfig ac){
		authenticator = ac;
	}

	
	public IAuthenticator getAuthenticator(){
		if (authenticatorInstance == null) {
			authenticatorInstance = (IAuthenticator)ClazzLoader.getInstanceOf(authenticator.getClazz());
			if (authenticatorInstance != null) authenticatorInstance.configure(authenticator);
		}
		return authenticatorInstance;
	}
	
	public ISessionManager getSessionManager() {
		if (sessionManagerInstance == null) {
			sessionManagerInstance = (ISessionManager)ClazzLoader.getInstanceOf(sessionManager.getClazz());
			if (sessionManagerInstance != null) sessionManagerInstance.configure(sessionManager);
		}
		return sessionManagerInstance;
	}


	public Date getLoadedTS() {
		return loadedTS;
	}


	public void setLoadedTS(Date loadedTS) {
		this.loadedTS = loadedTS;
	}
	
	
}
